package com.kalle.workflow.autoconfigure;

import com.kalle.workflow.engine.WorkflowEngine;
import com.kalle.workflow.engine.WorkflowEngineConfiguration;
import com.kalle.workflow.service.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class WorkflowAutoConfiguration implements InitializingBean {

    private final DataSource dataSource;

    public WorkflowAutoConfiguration(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void afterPropertiesSet() {

    }

    @Bean
    @ConditionalOnMissingBean
    public WorkflowEngineConfiguration workFlowEngineConfiguration() {
        WorkflowEngineConfiguration workflowEngineConfiguration = new WorkflowEngineConfiguration();
        workflowEngineConfiguration.setDataSource(dataSource);
        return workflowEngineConfiguration;
    }

    @Bean
    @ConditionalOnMissingBean
    public WorkflowEngine workflowEngine() {
        return workFlowEngineConfiguration().getWorkflowEngine();
    }

    @Bean
    @ConditionalOnMissingBean
    public DeploymentService deploymentService() {
        return workflowEngine().getDeploymentService();
    }

    @Bean
    @ConditionalOnMissingBean
    public TaskService taskService() {
        return workflowEngine().getTaskService();
    }

    @Bean
    @ConditionalOnMissingBean
    public SequenceFlowService sequenceFlowService() {
        return workflowEngine().getSequenceFlowService();
    }

    @Bean
    @ConditionalOnMissingBean
    public ProcessService processService() {
        return workflowEngine().getProcessService();
    }

    @Bean
    @ConditionalOnMissingBean
    public FlowService flowService() {
        return workflowEngine().getFlowService();
    }

}
